package com.relax.mybatis.plugin.sensitive.strategy;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.relax.mybatis.constant.SensitiveType;
import com.relax.mybatis.plugin.sensitive.strategy.impl.AddressSensitiveStrategy;
import com.relax.mybatis.plugin.sensitive.strategy.impl.BankCardSensitiveStrategy;
import com.relax.mybatis.plugin.sensitive.strategy.impl.CarNumberSensitiveStrategy;
import com.relax.mybatis.plugin.sensitive.strategy.impl.ChineseNameSensitiveStrategy;
import com.relax.mybatis.plugin.sensitive.strategy.impl.DefaultSensitiveStrategy;
import com.relax.mybatis.plugin.sensitive.strategy.impl.EmailSensitiveStrategy;
import com.relax.mybatis.plugin.sensitive.strategy.impl.IdCardSensitiveStrategy;
import com.relax.mybatis.plugin.sensitive.strategy.impl.MobileSensitiveStrategy;
import com.relax.mybatis.plugin.sensitive.strategy.impl.NoneSensitiveStrategy;
import com.relax.mybatis.plugin.sensitive.strategy.impl.PasswordSensitiveStrategy;
import com.relax.mybatis.plugin.sensitive.strategy.impl.PhoneSensitiveStrategy;

/**
 * @author relax
 * @date 2022/07/05
 * @desc 脱敏策略上下文
 */
public class SensitiveContext {

    private static final Map<String, SensitiveStrategy> STRATEGY_MAP = new ConcurrentHashMap<>();

    public SensitiveContext() {
        resetStrategyMap();
    }

    /**
     * 重置策略集合
     */
    private void resetStrategyMap() {
        STRATEGY_MAP.clear();
        STRATEGY_MAP.put(SensitiveType.NONE, new NoneSensitiveStrategy());
        STRATEGY_MAP.put(SensitiveType.DEFAUL, new DefaultSensitiveStrategy());
        STRATEGY_MAP.put(SensitiveType.CHINESE_NAME, new ChineseNameSensitiveStrategy());
        STRATEGY_MAP.put(SensitiveType.ID_CARD, new IdCardSensitiveStrategy());
        STRATEGY_MAP.put(SensitiveType.PHONE, new PhoneSensitiveStrategy());
        STRATEGY_MAP.put(SensitiveType.MOBILE, new MobileSensitiveStrategy());
        STRATEGY_MAP.put(SensitiveType.ADDRESS, new AddressSensitiveStrategy());
        STRATEGY_MAP.put(SensitiveType.EMAIL, new EmailSensitiveStrategy());
        STRATEGY_MAP.put(SensitiveType.BANK_CARD, new BankCardSensitiveStrategy());
        STRATEGY_MAP.put(SensitiveType.CAR_NUMBER, new CarNumberSensitiveStrategy());
        STRATEGY_MAP.put(SensitiveType.PASSWORD, new PasswordSensitiveStrategy());
    }

    /**
     * 获取策略
     * 
     * @param type
     * @return
     */
    public static SensitiveStrategy getStrategy(String type) {
        SensitiveStrategy strategy = STRATEGY_MAP.get(type);
        if (strategy == null) {
            throw new IllegalArgumentException("none sensitive strategy found, type: " + type);
        }
        return strategy;
    }

    /**
     * 添加自定义策略
     * 
     * @param type
     * @param strategy
     * @return
     */
    public static void addStrategy(String type, SensitiveStrategy strategy) {
        STRATEGY_MAP.put(type, strategy);
    }

}
